using System;
using System.Collections.Generic;
using System.Web;
using Tools.Common.UnitOfWork.Contracts;

namespace Web.App.Infrastructure.UnitOfWork.Implementations
{
    public class StandardUoWRepository : IUnitOfWorkRepository
    {
        private IDictionary<Guid, IUnitOfWork> items = new Dictionary<Guid, IUnitOfWork>();

        public IUnitOfWork PickupUnitOfWork(Guid? id)
        {
            IUnitOfWork uow = null;

            if (id != null)
            {
                uow = items[(Guid)id];
                if (uow != null)
                    items.Remove((Guid)id);
            }

            return uow;
        }

        public void SaveUnitOfWork(IUnitOfWork unitOfWork)
        {
            if (unitOfWork == null)
                throw new ArgumentNullException("uow must not be null");

            items[unitOfWork.ID] = unitOfWork;
        }

        public void DisposeUnitOfWork(Guid? id)
        {
            IUnitOfWork uow = null;

            if (id != null)
            {
                uow = items[(Guid)id];

                if (uow != null)
                    uow.Dispose();
            }


        }
    }
}